package cn.hxzy.icon.icon.controller;

import cn.hxzy.icon.icon.entity.Role;
import cn.hxzy.icon.icon.entity.User;
import cn.hxzy.icon.icon.entity.po.UserPo;
import cn.hxzy.icon.icon.entity.vo.PageBean;
import cn.hxzy.icon.icon.entity.vo.ReturnBean;
import cn.hxzy.icon.icon.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询全部+分页
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("list")
    public ReturnBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer size) {
        PageBean<User> all = userService.findAll(new PageBean<User>(page, size));
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * 添加用户
     *
     * @param user
     * @param roleId
     * @return
     */
    @PostMapping("add")
    public ReturnBean add(User user, Integer roleId) {
        System.out.println(user);
        if (roleId == null) {
            Role role = new Role();
            role.setId(5);
            user.setRole(role);
        } else {
            Role role = new Role();
            role.setId(roleId);
            user.setRole(role);
        }
        if (user != null) {
            userService.add(user);
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.ADD_ERROR);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("deleteId")
    public ReturnBean delete(Integer id) {
        userService.deleteById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 根据id查用户
     *
     * @param id
     * @return
     */
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        User user = userService.FindById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, user);
    }

    /**
     * 名字查询
     *
     * @param name
     * @return
     */
    @PostMapping("findByName")
    public ReturnBean findByNameLike(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer size,String name) {
        PageBean<User> all = userService.findByNameLike(new PageBean<User>(page, size),name);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }


    /**
     * 修改用户
     *
     * @param userPo
     * @param roleId
     * @return
     */
    @PostMapping("update")
    public ReturnBean update(UserPo userPo, Integer roleId) {
        User user = new User();
        BeanUtils.copyProperties(userPo, user);
        Role role = new Role();
        role.setId(roleId);
        user.setRole(role);
        userService.update(user);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);

    }

}

